const mockBrowserStorageLocal = {
  // 设置数据
  set(items) {
    return new Promise((resolve, reject) => {
      try {
        for (const key in items) {
          if (items.hasOwnProperty(key)) {
            const value = JSON.stringify(items[key]);
            localStorage.setItem(key, value);
          }
        }
        resolve();
      } catch (error) {
        reject(error);
      }
    });
  },
  // 获取数据
  get(keys) {
    return new Promise((resolve, reject) => {
      const result = {};
      try {
        if (typeof keys === 'string') {
          const value = localStorage.getItem(keys);
          result[keys] = value ? JSON.parse(value) : null;
        } else if (Array.isArray(keys)) {
          keys.forEach((key) => {
            const value = localStorage.getItem(key);
            result[key] = value ? JSON.parse(value) : null;
          });
        } else if (typeof keys === 'object' && keys !== null) {
          for (const key in keys) {
            if (keys.hasOwnProperty(key)) {
              const value = localStorage.getItem(key);
              result[key] = value ? JSON.parse(value) : keys[key];
            }
          }
        }
        resolve(result);
      } catch (error) {
        reject(error);
      }
    });
  },
  // 删除数据
  remove(keys) {
    return new Promise((resolve, reject) => {
      try {
        if (typeof keys === 'string') {
          localStorage.removeItem(keys);
        } else if (Array.isArray(keys)) {
          keys.forEach((key) => {
            localStorage.removeItem(key);
          });
        }
        resolve();
      } catch (error) {
        reject(error);
      }
    });
  },
  // 清除所有数据
  clear() {
    return new Promise((resolve, reject) => {
      try {
        localStorage.clear();
        resolve();
      } catch (error) {
        reject(error);
      }
    });
  },
};

export default {
  runtime: {
    getManifest() {
      return {
        version: '1.29.9',
      };
    },
    onMessage: {
      addListener() {},
      getURL(url) {
        return url;
      },
    },
    sendMessage() {},
  },
  // storage: new ExtensionStorageSimulator(),
  storage: {
    local: {
      onChanged: {
        addListener() {},
        removeListener() {},
      },
      set: mockBrowserStorageLocal.set,
      get: mockBrowserStorageLocal.get,
      remove: mockBrowserStorageLocal.remove,
    },
  },
  // tabs: {
  // query() {
  //   return [];
  // },
  // onCreated: {
  //   addListener() {},
  // },
  // onActivated: {
  //   addListener() {},
  // },
  // onCommitted: {
  //   addListener() {},
  // },
  // },
  // webNavigation: {
  //   onCommitted: {
  //     removeListener() {},
  //   },
  // },
};
